describe("防疫-告警通知", () => {
  beforeEach("", () => {
    const username = "admin";
    const password = "123456";
    cy.login(username, password);
    cy.intercept("GET", "/api/robot-abnormal-users/v1/list*").as(
      "getAbnormalList"
    );
    cy.visit("/disinfect/notification/strategy");
  });

  context("告警通知-页面显示", () => {
    it("进入告警策略", () => {
      cy.wait("@getAbnormalList");
      // 判断页面表头信息是否正确
      let tableTh = ["告警级别", "开启通知", "通知渠道", "接收人", "操作"];
      handleDataNumber(tableTh);
    });

    it("进入告警联系人", () => {
      getAbnormaUserList();
      // 判断页面表头信息是否正确
      let tableTh = [
        "联系人",
        "接收渠道",
        "备注",
        "联系手机",
        "公司名称",
        "推送内容",
        "Webhook",
        "加密签名",
        "创建时间",
        "操作",
      ];
      handleDataNumber(tableTh);
    });
  });

  it("添加告警", () => {
    cy.contains("添加告警").click();
    cy.intercept("POST", "/api/robot-abnormal-users/v1/create").as(
      "createAbnormal"
    );
    notificationBtn("告警级别").contains("普通").click();
    notificationBtn("开启通知").find("span").click();
    notificationBtn("通知渠道").contains("微信").click();
    cy.intercept("GET", "/api/abnormal-user/v1/list*").as(
      "getAbnormalUserList"
    );
    cy.get("form").find("button").click();
    cy.wait("@getAbnormalUserList");
    cy.contains("接收人列表");

    let tableTh = ["", "联系人", "接收渠道"];
    cy.getByData("recipient-list")
      .find("th")
      .should("have.length", tableTh.length);

    for (let i = 1; i < tableTh.length; i++) {
      cy.getByData("recipient-list").contains(tableTh[i]);
    }
    // 判断表格数据条数是否大于0
    cy.getByData("recipient-list")
      .find("tbody>tr")
      .then(($tr) => {
        expect($tr.length).to.greaterThan(0);
      });
    cy.getByData("recipient-list").find("tbody>tr>td>div").first().click();
    cy.getByData("recipient-list").parent().next().contains("确定").click();

    getAbnormalList();
    cy.contains("确定").click();
    cy.wait("@createAbnormal");
    cy.contains("操作成功");

    cy.wait("@getAbnormalList");
  });

  it("编辑告警", () => {
    cy.intercept("GET", "/api/robot-abnormal-users/v1/getOne/*").as(
      "getAbnormalOne"
    );
    cy.contains("编辑").click();
    cy.wait("@getAbnormalOne");
    notificationBtn("告警级别").contains("警告").click();
    notificationBtn("开启通知").find("span").click();
    notificationBtn("通知渠道").should("not.be.disabled");
    cy.get("form").contains("接收人").should("not.be.disabled");

    cy.intercept("PUT", "/api/robot-abnormal-users/v1/update").as(
      "updateAbnormal"
    );
    getAbnormalList();

    cy.contains("确定").click();
    cy.wait("@updateAbnormal");
    cy.contains("操作成功");

    cy.wait("@getAbnormalList");
  });

  it("删除告警", () => {
    cy.intercept("DELETE", "/api/robot-abnormal-users/v1/delete/*").as(
      "deleteAbnormal"
    );
    cy.contains("删除").click();
    getAbnormalList();
    cy.contains("确认继续操作吗")
      .closest(".el-message-box")
      .contains("确定")
      .click();

    cy.wait("@deleteAbnormal")
      .its("response.body")
      .then(($res) => {
        const message = $res.message;
        cy.contains(`${message}`);
      });

    cy.wait("@getAbnormalList");
  });

  it("添加联系人", () => {
    getAbnormaUserList();
    cy.contains("添加联系人").click();
    notificationBtn("公司名称").click();
    cy.getByData("company-list").should(($li) => {
      expect($li.length).to.be.greaterThan(0);
    });
    cy.getByData("company-list").first().click();
    notificationBtn("联系人").type("测试人1");
    notificationBtn("备注").type("测试");
    notificationBtn("接收渠道").contains("短信").click();
    notificationBtn("联系手机").type("12345678901");

    cy.intercept("POST", "/api/abnormal-user/v1/create").as(
      "createAbnormalUser"
    );

    cy.contains("确定").click();

    cy.wait("@createAbnormalUser");
    cy.contains("操作成功");
    cy.wait("@getAbnormaUserList");
  });

  it.only("编辑联系人", () => {
    getAbnormaUserList();
    cy.intercept("GET", "/api/abnormal-user/v1/getById/*").as(
      "getAbnormalUserOne"
    );
    cy.get("table>tbody>tr").eq(-1).contains("编辑").click();
    cy.wait("@getAbnormalUserOne");

    notificationBtn("公司名称").click();
    cy.getByData("company-list").should(($li) => {
      expect($li.length).to.be.greaterThan(0);
    });
    cy.getByData("company-list").eq(-1).click();
    notificationBtn("联系人").clear();
    notificationBtn("联系人").type("测试人2");
    notificationBtn("备注").clear();
    cy.get("form").contains("接收渠道").should("not.be.disabled");
    // notificationBtn("联系手机").should("not.be.disabled");
    // notificationBtn("联系手机").type("88888888888");

    cy.intercept("PUT", "/api/abnormal-user/v1/update").as(
      "updateAbnormalUser"
    );

    cy.contains("确定").click();

    cy.wait("@updateAbnormalUser");
    cy.contains("操作成功");
    cy.wait("@getAbnormaUserList");
  });

  it("删除告警联系人", () => {
    getAbnormaUserList();

    cy.intercept("DELETE", "/api/abnormal-user/v1/delete/*").as(
      "deleteAbnormalUser"
    );

    cy.get("table>tbody>tr").eq(-1).contains("删除").click();

    getAbnormalList();

    cy.contains("确认继续操作吗")
      .closest(".el-message-box")
      .contains("确定")
      .click();

    cy.wait("@deleteAbnormalUser")
      .its("response.body")
      .then(($res) => {
        const message = $res.message;
        cy.contains(`${message}`);
      });

    cy.wait("@getAbnormaUserList");
  });
});

function notificationBtn(label) {
  return cy.contains("label", label).siblings();
}

function getAbnormalList() {
  cy.intercept("GET", "/api/robot-abnormal-users/v1/list*").as(
    "getAbnormalList"
  );
}

function getAbnormaUserList() {
  cy.intercept("GET", "/api/abnormal-user/v1/list*").as("getAbnormaUserList");
  cy.visit("/disinfect/notification/user");
  cy.wait("@getAbnormaUserList");
}

function handleDataNumber(tableTh) {
  cy.get("table>thead>tr>th").should("have.length", tableTh.length);
  for (let i = 0; i < tableTh.length; i++) {
    cy.contains(tableTh[i]);
  }
  // 判断表格数据条数是否大于0
  cy.get("table>tbody>tr").then(($tr) => {
    expect($tr.length).to.greaterThan(0);
  });
}
